//Create global namespace Happy
if (typeof Happy == "undefined" || !Happy) {
  var Happy = {};
}

//Module for $=jQuery
(function($) {
//Create namespace Happy.LabbelCloud
Happy.LabelCloud = (function(){
  //Private members
  
  //Regular expression for label count
  var regexCount = /\((\d+)\)/;
  
  //Public members
  return {
    //font size range
    MAX_SIZE: 25,
    MIN_SIZE: 13,
    //font color range
    MAX_COLOR: [255,74,74],
    MIN_COLOR: [40,200,20],
    //Element id to apply label cloud
    TARGET_ID: "#Label1",
    
    //Weighting the style value according count and maxCount
    weightStyle: function( min, max, count, maxCount ) {
      if (maxCount <= 1 || min === max ) {
        return max;
      } else if (min < max) {
        return min + Math.floor(Math.log(count) * (max-min) / Math.log(maxCount));
      } else {
        return min - Math.floor(Math.log(count) * (min-max) / Math.log(maxCount));
      }//end of else
    },
    
    weightStyleSectional: function( min, max, count, maxCount ) {
      if (maxCount <= 1 || min === max ) {
        return max;
      } else {
        //Scale down count smaller than (0.66*maxCount) to make more close-to-MIN_COLORed labels 
        if (count < maxCount*0.66) {
          count = (count-1)*0.3 + 1;
        }
        if (min < max) {
          return min + Math.floor(Math.log(count) * (max-min) / Math.log(maxCount));
        } else {
          return min - Math.floor(Math.log(count) * (min-max) / Math.log(maxCount));
        }
      }//end of else
    },
    
    cloudIt: function(weightFn) {
      //Check weighting funciton
      if (!$.isFunction(weightFn)) {
        return;
      }
      
      //Label link jQuery objects
      var labelObjs = [];
      //Label counts
      var labelCounts = [];
      //Maximum label count
      var maxCount = 0;      
      var aObj, sObj, count, s, r, g, b;    
      
      //find targets
      $(this.TARGET_ID + " li").each(function(){
        //Label link
        aObj = $(this).find("a");
        //Label count
        sObj = $(this).find("span");
        count = parseInt(regexCount.exec(sObj.text())[1]);
        labelObjs.push(aObj);
        labelCounts.push(count);
        if (count > maxCount) maxCount = count;
      });
      
      //add style
      $(this.TARGET_ID + " div.widget-content").addClass("label-cloud");
      for(var i=0; i<labelObjs.length; i++) {
        aObj = labelObjs[i];
        count = labelCounts[i];
        s = weightFn( this.MIN_SIZE, this.MAX_SIZE, count, maxCount );
        r = weightFn( this.MIN_COLOR[0], this.MAX_COLOR[0], count, maxCount );
        g = weightFn( this.MIN_COLOR[1], this.MAX_COLOR[1], count, maxCount );
        b = weightFn( this.MIN_COLOR[2], this.MAX_COLOR[2], count, maxCount );
              
        aObj.css('color', 'RGB('+r+','+g+','+b+')').attr('title', count + ' posts').parent("li").css('font-size', s + "px");;
      }//end of for
      
    }//end of function
  };//end of return
})();
})(jQuery);